/**
 * Copyright (c) 2015 Baidu. All Rights Reserved.
 *
 * @file 用于复制对象
 * @author yuanguozheng
 */

/**
 * 复制对象
 *
 * @param {Object} obj 需要复制的对象
 * @return {Object} 复制的对象
 */
function objCopier(obj) {
    var o;
    switch (typeof obj) {
        case 'undefined':
            break;
        case 'string':
            o = obj + '';
            break;
        case 'number':
            o = obj - 0;
            break;
        case 'boolean':
            o = obj;
            break;
        case 'object':
            procObj();
            break;
        default:
            o = obj;
            break;
    }

    /**
     * 处理对象
     */
    function procObj() {
        if (obj === null) {
            o = null;
        } else {
            if (obj instanceof Array) {
                o = [];
                for (var i = 0, len = obj.length; i < len; i++) {
                    o.push(objCopier(obj[i]));
                }
            } else {
                o = {};
                for (var k in obj) {
                    if (obj.hasOwnProperty(k)) {
                        o[k] = objCopier(obj[k]);
                    }
                }
            }
        }
    }
    return o;
}

module.exports = objCopier;